package com.mallify.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mallify.common.constants.Constants;
import com.mallify.common.model.system.SystemAttachment;
import com.mallify.service.mapper.SystemAttachmentMapper;
import com.mallify.service.service.SystemAttachmentService;
import com.mallify.service.service.SystemConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * SystemAttachmentServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * | HANSON [ HANSON赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Licensed HANSON并不是自由软件，未经许可不能去掉HANSON相关版权
 * +----------------------------------------------------------------------
 * | Author: HANSON Team <hanson0622@126.com>
 * +----------------------------------------------------------------------
 */
@Service
public class SystemAttachmentServiceImpl extends ServiceImpl<SystemAttachmentMapper, SystemAttachment>
        implements SystemAttachmentService {

    @Resource
    private SystemAttachmentMapper systemAttachmentMapper;

    @Resource
    private SystemConfigService systemConfigService;

    /**
     * 清除 cdn url， 在保存数据的时候使用
     *
     * @param path String 文件路径
     * @return String
     */
    @Override
    public String clearPrefix(String path) {
        if (StringUtils.isBlank(path)) {
            return path;
        }
        Constants.CND_URL = getCdfUrl();
        if (path.contains(getCdfUrl() + "/")) {
            return path.replace(getCdfUrl() + "/", "");
        }
        return path;
    }

    /**
     * 获取cdn url
     *
     * @return String
     */
    private String getCdfUrl() {
        String uploadType = systemConfigService.getValueByKeyException("uploadType");
        //获取配置信息
        int type = Integer.parseInt(uploadType);
        String pre = "local";
        switch (type) {
            case 2:
                pre = "qn";
                break;
            case 3:
                pre = "al";
                break;
            case 4:
                pre = "tx";
                break;
            default:
                break;
        }
        return systemConfigService.getValueByKey(pre + "UploadUrl");
    }
}